package com.my.test.string;

public class ReverseString {
	
	/**
	 * Reverse a string: "we test coders" to "ew tset sredoc"
	 * @param str
	 * @return
	 */
	public static String stringReverse(String str) {
		//we test coders -> sredoc tset ew -> 
		StringBuilder buf = new StringBuilder();
		for(int i=str.length()-1; i>=0; i--) {
			buf.append(str.charAt(i));
		}
		String[] words = buf.toString().split(" ");
		StringBuilder buf2 = new StringBuilder();
		for(int j = words.length-1; j>0; j--) {
			buf2.append(words[j]).append(" ");
		}
		buf2.append(words[0]);
		return buf2.toString();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("we test coders -> " + stringReverse("we test coders"));
//		System.out.println("we -> " + stringReverse("we "));
	}

}
